const path = require('path')
const dirname = '/app/scrapy_service/scrapy_service/'
const jsDomPath = path.resolve(dirname, 'commons/jsDom.js');
const {dom, a1} = require(jsDomPath)
// const axios = require('axios');
window = dom.window;

require(path.resolve(dirname, 'commons/js/Sanji.js'))

const {
    genTraceId,
    encrypt_mcr,
    encrypt_b64Encode,
    encrypt_encodeUtf8
} = require(path.resolve(dirname, 'commons/encrypt.js'))

const sign = function (url, data) {
    if (data == "undefined") {
        data = undefined;
    }

    // console.log(url)
    const l = window._webmsxyw(url, data)

    const t = l['X-t']
    const s = l['X-s']

    const b1 = "I38rHdgsjopgIvesdVwgIC+oIELmBZ5e3VwXLgFTIxS3bqwErFeexd0ekncAzMFYnqthIhJeDnMDKutRI3KsYorWHPtGrbV0P9WfIi/eWc6eYqtyQApPI37ekmR1QL+5Ii6sdnoeSfqYHqwl2qt5BfqJIvFbNLQ+ZPw7Ixdsxuwr4qtkIkrwIi/skZc3ICLdI3Oe0utl2ADZsL5eDSJsSPwXIEvsiVtJOPw8BuwfPpdeTDWOIx4VIiu6ZPwbJqt0IxHyoMAeVutWIvvs1VtnIi+KIEzaeo6s09G1e05sYuttrboe0FFWp9Ke0YqQIx/eDPwmIiJefqtAzZVVOsuwI3deTutA/Yve67zGIhE8IEY2NoqWI3Ty8IYgIEhIBuwSIChV+/Kedp5e3qtuI36sja7s0fH4Ik5eirm5KqwfIiKsTove1SKs3PwPmeOedqwVI34LaU6eSqwkzgdsWVt+I3kKIxmdsqtmrPtBnnI4mAAsDfgexVwbIhJednOsxzZ="

    const d = {
        s0: 3,
        s1: "",
        x0: "1",
        x1: "3.6.8",
        x2: 'Mac OS',
        x3: "xhs-pc-web",
        x4: "4.1.6",
        x5: a1,
        x6: t,
        x7: s,
        x8: b1,
        x9: encrypt_mcr(t + "" + s + a1),
        x10: 888
    };
    return {
        xt: t,
        xs: s,
        traceId: genTraceId(),
        xsCommon: encrypt_b64Encode(encrypt_encodeUtf8(JSON.stringify(d))),
        cookie: window.document.cookie
    }
}
const ts = () => {
    const base_url = 'https://edith.xiaohongshu.com'
    const api = "/api/sns/web/v2/comment/page?note_id=641e96f6000000000800e57b&cursor=641fbbe5000000001700494a&top_comment_id=&image_formats=jpg,webp,avif"
    const data = undefined;
    const result = sign(api, data)
    const headers = {
        'cookie': result['cookie'],
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
        'x-b3-traceid': result['traceId'],
        'x-s': result['xs'],
        'x-s-common': result['xsCommon'],
        'x-t': result['xt'].toString(),
        'authority': 'edith.xiaohongshu.com',
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'content-type': 'application/json;charset=UTF-8',
        'origin': 'https://www.xiaohongshu.com',
        'pragma': 'no-cache',
        'referer': 'https://www.xiaohongshu.com/',
        'sec-ch-ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
    }
    // const response = axios.get(base_url + api, {
    //     headers: headers
    // })
    //     .then(response => {
    //         console.log(response.data['data']['comments']);
    //     })
    //     .catch(error => {
    //         console.error(error);
    //     });

    console.log(sign(api, data));
}
// ts()

module.exports = sign;

